package com.bdth.mixapi.api.controller.index;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdth.mixapi.api.entity.Device;
import com.bdth.mixapi.api.entity.DeviceChange;
import com.bdth.mixapi.api.service.DeviceChangeService;
import com.bdth.mixapi.api.service.DeviceService;
import com.bdth.mixapi.api.vo.*;
import com.bdth.mixapi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-11
 */
@Api("设备位置表")
@RestController
@RequestMapping("/index")
public class DeviceController {
    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceChangeService deviceChangeService;

    @ApiOperation(value="获取上一天设备位置列表", notes="")
    @RequestMapping(value = "device/list", method = RequestMethod.GET)
    public CommonList<Dvo> list(){
        //查询有数据的最后一天
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("uptime");
        wrapper.last("limit 1");
        List<Device> list = deviceService.list(wrapper);
        List<Dvo> l = new ArrayList<>();

        if(list!=null && list.size()>0) {
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uptime",DateUtil.format(list.get(0).getUptime()));
            queryWrapper.orderByAsc("type");
            List<Device> deviceList = deviceService.list(queryWrapper);
            for(int i=0;i<deviceList.size();i++){
                Dvo d = new Dvo();
                d.setP(deviceList.get(i).getType());
                d.setT(deviceList.get(i).getLat());
                d.setG(deviceList.get(i).getLng());
                l.add(d);
            }
            return CommonList.success(l);
        }else {
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value = "删除N天前的设备信息")
    @RequestMapping(value = "device/clear", method = RequestMethod.GET)
    public CommonMessage clearData(@RequestParam int day){
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        String date = DateUtil.getLastDate(day);
        queryWrapper.le("uptime",date);
        boolean flag = deviceService.remove(queryWrapper);
        if(flag) {
            return CommonMessage.success("删除数据成功");
        }else{
            return CommonMessage.fail("删除失败");
        }
    }

    @ApiOperation(value = "删除设备位置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "date", dataType = "String", required = true, value = "日期"),
    })
    @RequestMapping(value = "device/clearbydate", method = RequestMethod.GET)
    public CommonMessage clearByDate(@RequestParam String date){
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uptime",date);
        boolean flag = deviceService.remove(queryWrapper);
        if(flag) {
            return CommonMessage.success("删除数据成功");
        }else{
            return CommonMessage.fail("删除失败");
        }
    }

    @ApiOperation(value = "获取设备位置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "date", dataType = "String", required = true, value = "日期"),
    })
    @RequestMapping(value = "device/getbydate", method = RequestMethod.GET)
    public CommonList<Device> getByDate(@RequestParam String date){
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uptime",date);
        queryWrapper.orderByAsc("type");
        //queryWrapper.eq("type","3");
        List<Device> list = deviceService.list(queryWrapper);
        if(list!=null && list.size()>0) {
            return CommonList.success(list);
        }else{
            return CommonList.fail("没有查询到数据");
        }
    }

    @ApiOperation(value = "自定义插入数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "srcdate", dataType = "String", required = true, value = "源数据日期"),
            @ApiImplicitParam(paramType = "query", name = "resdate", dataType = "String", required = true, value = "插入日期"),
            @ApiImplicitParam(paramType = "query", name = "type", dataType = "String", required = true, value = "数据类型"),
            @ApiImplicitParam(paramType = "query", name = "delete", dataType = "int", required = true, value = "删除数据数量"),
    })
    @RequestMapping(value = "device/createdata", method = RequestMethod.GET)
    public CommonMessage createData(@RequestParam String srcdate,@RequestParam String resdate,@RequestParam Integer type,@RequestParam Integer delete){
        //查询Device源数据日期的数据
        QueryWrapper<Device> srcquery = new QueryWrapper<>();
        srcquery.eq("uptime",srcdate);
        srcquery.eq("type",type);
        List<Device> srclist = deviceService.list(srcquery);
        System.out.println("startsum === "+srclist.size());
        for(int i=0;i<delete;i++){
            Random ran=new Random();
            int sum = ran.nextInt(srclist.size());
            srclist.remove(sum);
        }
        System.out.println("endsum === "+srclist.size());
        Date resD = DateUtil.toDate(resdate);
        for(int i=0;i<srclist.size();i++){
            srclist.get(i).setUptime(resD);
        }
        boolean f = deviceService.saveBatch(srclist);
        return CommonMessage.message(f?"200":"500",f?"插入成功":"插入失败");
    }

    @ApiOperation(value = "将增加的数据插入CHANGE表中")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "srcdate", dataType = "String", required = true, value = "源数据日期"),
            @ApiImplicitParam(paramType = "query", name = "resdate", dataType = "String", required = true, value = "比较日期"),
            @ApiImplicitParam(paramType = "query", name = "chdate", dataType = "String", required = true, value = "插入日期"),
            @ApiImplicitParam(paramType = "query", name = "type", dataType = "String", required = true, value = "数据类型")
    })
    @RequestMapping(value = "device/createChangeData", method = RequestMethod.GET)
    public CommonMessage createChangeData(@RequestParam String srcdate,@RequestParam String resdate,@RequestParam String chdate,@RequestParam String type){
        //查询Device源数据日期的数据
        List<Device> srclist = deviceService.getChangeData(srcdate,resdate,type);
        List<DeviceChange> chlist = new ArrayList<>();
        //组装change数据
        for(int i=0;i<srclist.size();i++){
            Device d = srclist.get(i);
            DeviceChange ch = new DeviceChange(chdate,d.getDevice(),type,d.getLng(),d.getLat());
            chlist.add(ch);
        }
        boolean f = deviceChangeService.saveBatch(chlist);
        return CommonMessage.message(f?"200":"500",f?"插入成功":"插入失败");
    }

    @ApiOperation(value = "获取历史演变位置信息")
    @RequestMapping(value = "device/lsyb", method = RequestMethod.GET)
    public CommonResult<DeviceChangeVo> getListChange(){
        DeviceChangeVo result = new DeviceChangeVo();
        List<CrTimeVo> times = deviceChangeService.getTimes();
        List<String> rts = new ArrayList<>();
        for(int i=0;i<times.size();i++){
            rts.add(times.get(i).getCrtime());
        }
        result.setTimes(rts);
        List<DeviceVo> list = new ArrayList<>();
        for(int i=0;i<times.size();i++){
            String time = times.get(i).getCrtime();
            DeviceVo deviceVo = new DeviceVo();
            deviceVo.setTime(time);
            QueryWrapper<DeviceChange> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("crtime",time);
            List<DeviceChange> l = deviceChangeService.list(queryWrapper);
            deviceVo.setList(l);
            list.add(deviceVo);
        }
        result.setResult(list);
        return CommonResult.success(result);
    }

    @ApiOperation(value = "生成TYPE3")
    @RequestMapping(value = "device/crtType3", method = RequestMethod.GET)
    public CommonMessage createType3(){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("type","2");
        wrapper.eq("uptime","2019-04-28");
        List<Device> all = deviceService.list(wrapper);
        List<Device> type3 = new ArrayList<>();
        for(int i=0;i<3019;i++){
            Random ran=new Random();
            int sum = ran.nextInt(all.size());
            Device bean = new Device();
            Device b = all.get(i);
            bean.setUptime(b.getUptime());
            bean.setId(b.getId());
            bean.setDevice(b.getDevice()+i);
            bean.setType(3);
            Random r = new Random();
            double s = (1+ran.nextInt(9))*0.0002;
            bean.setLng(b.getLng().add(new BigDecimal(s)));
            bean.setLat(b.getLat().add(new BigDecimal(s)));
            type3.add(bean);
        }
        boolean f = deviceService.saveBatch(type3);
        return CommonMessage.message(f?"200":"500",f?"插入成功":"插入失败");
    }

    /**
     * 每天生成TYpe3
     * @param d
     * @return
     */
    @ApiOperation(value = "生成TYPE3")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "d", dataType = "String", required = true, value = "插入日期")
    })
    @RequestMapping(value = "device/type3", method = RequestMethod.GET)
    public CommonMessage createNowType(@RequestParam String d){
        //查询有数据的最后一天
        QueryWrapper<Device> timequery = new QueryWrapper<>();
        timequery.orderByDesc("uptime");
        timequery.eq("type","3");
        timequery.last("limit 1");
        List<Device> list = deviceService.list(timequery);
        String now = null;
        if(list!=null&&list.size()>0) {
            now = DateUtil.format(list.get(0).getUptime());
        }
        QueryWrapper<Device> type3query = new QueryWrapper<>();
        type3query.eq("uptime",now);
        type3query.eq("type", "3");
        //最近一天的type3的数据
        List<Device> dl = deviceService.list(type3query);
        List<Device> result = new ArrayList<>();
        for(int i=0;i<dl.size();i++){
            Device dd = dl.get(i);
            dd.setUptime(DateUtil.toDate(d));
            result.add(dd);
        }
        //从type2中获取N条数据插入到dl中
        List<Device> tl2 = getType2(d);
        for(int i=0;i<tl2.size();i++){
            dl.add(tl2.get(i));
        }
        boolean f = deviceService.saveBatch(dl);
        return CommonMessage.message(f?"200":"500",f?"插入成功":"插入失败");
    }

    private List<Device> getType2(String d){
        //查询有数据的最后一天
        QueryWrapper<Device> timequery = new QueryWrapper<>();
        timequery.orderByDesc("uptime");
        timequery.eq("type","2");
        timequery.last("limit 1");
        List<Device> list = deviceService.list(timequery);
        String now = null;
        if(list!=null&&list.size()>0) {
            now = DateUtil.format(list.get(0).getUptime());
        }
        QueryWrapper<Device> type2query = new QueryWrapper<>();
        type2query.eq("uptime",now);
        type2query.eq("type", "2");
        //最近一天的type3的数据
        List<Device> dl = deviceService.list(type2query);
        List<Device> type3 = new ArrayList<>();
        Random rr = new Random();
        int size = 20+rr.nextInt(30);
        for(int i=0;i<size;i++){
            Random ran=new Random();
            int sum = ran.nextInt(dl.size());
            Device bean = new Device();
            Device b = dl.get(i);
            bean.setUptime(DateUtil.toDate(d));
            bean.setId(b.getId());
            bean.setDevice(b.getDevice()+i);
            bean.setType(3);
            Random r = new Random();
            double s = (1+ran.nextInt(9))*0.0002;
            bean.setLng(b.getLng().add(new BigDecimal(s)));
            bean.setLat(b.getLat().add(new BigDecimal(s)));
            type3.add(bean);
        }
        return type3;
    }
}

